<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <base target="_self">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache,must-ridate">
    <meta http-equiv="expires" content="0">
    <title></title>
    <link rel="stylesheet" type="text/css" href="/static/plugins/bootstrap/css/bootstrap.min.css" />
    <link rel="stylesheet" type="text/css" href="/static/plugins/bootstrap-table/bootstrap-table.css" />
    <script type="text/javascript" src="/static/plugins/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" src="/static/plugins/bootstrap/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="/static/plugins/bootstrap-table/bootstrap-table.js"></script>
    <script type="text/javascript" src="/static/plugins/bootstrap-table/bootstrap-table-zh-CN.min.js"></script>
</head>

<style  type="text/css">
    .required {
        color: red;
    }       
</style>

<body>
    <div style="text-align: right;">
        <button type="button" id="btnSave" class="btn btn-danger">保 存</button>&nbsp;&nbsp;
    </div>
    <div id="changeysInfo">
                    
    </div>
    <div style="height: 10px;"></div>
    <div style="width: 100%; height: 30px; text-align: center; line-height: 30px; background-color: #C0C0C0;">
        <a href="#" onclick="addchangeys(0,'','','','');">点击添加更多变更与更正事项</a>
    </div>
</body>
    
    <script type="text/javascript">
        //初始化数据
        let changeData = new Object();
        $(function () {
            let uuid = GetQueryValue('uuid');
            let bsid = GetQueryValue('bsid');
            let regitem_id = GetQueryValue('regitem_id');
            let productid = GetQueryValue('productid');
            let productcode = GetQueryValue('productcode');

            //加载变更与更正事项
            find_app_dfs_zxd_cschange_by_uuid(uuid);

            //保存变更与更正事项
            $("#btnSave").click(function() {
				//保存之前必输项校验
				let value=requiredCheck();
				if(value=="0"){		
					savechangeys(uuid, bsid, regitem_id, productid, productcode);
				}
            });
        });

        //查询预登记-变更与更正事项要素
        function find_app_dfs_zxd_cschange_by_uuid(uuid) {
            jQuery.ajax({
                data: { "uuid": uuid },
                url: "/node/find_app_dfs_zxd_cschange_by_uuid/" + uuid,
                async: false,
                cache: false,
                success: function (data) {
                    if (typeof(data.length) != "undefined") {
                        $.each(data, function (index, item) {
                            addchangeys(item.id, item.change_type, item.change_bdesc, item.change_adesc, item.change_reason);
                        });
                    }
                }
            });
        }

        //添加变更与更正事项要素
        var q = 1;
        function addchangeys(id, change_type, change_bdesc, change_adesc,change_reason) {
            let str = `<div id="changeysDiv` + q + `"><div style="height: 10px;"></div>
                    <input type="hidden" name="changeysNo`+ q + `" value="` + q + `">
                    <input type="hidden" id="id`+ q + `" name="id` + q + `" value="` + id + `">
                    <table border="1" bordercolor="#C0C0C0" cellspacing="2" cellpadding="2"
                        style="width: 100%;">
                        <tr>
                            <td>
                                <span class="required">*</span>变更类型/更正字段:
                            </td>
                            <td>
                                <input type="text" id="change_type`+ q + `" name="change_type` + q + `" value="` + change_type + `" style="width: 210px;">
                            </td>
                            <td>
                                更正前值：
                            </td>
                            <td>
								<input type="text" id="change_bdesc`+ q + `" name="change_bdesc` + q + `" value="` + change_bdesc + `" style="width: 210px;">
								
                            </td>
                            <td>
                                <span class="required">*</span>变更事项/更正后值：
                            </td>
                            <td>
								<input type="text" id="change_adesc`+ q + `" name="change_adesc` + q + `" value="` + change_adesc + `" style="width: 210px;">
                            </td>
							</tr>
							<tr>
                            <td>
                                <span class="required">*</span>变更/更正原因：
                            </td>
                            <td colspan=5>
								<textarea cols="100" rows="3" id="change_reason`+ q + `" name="change_reason` + q + `" style="width:930px;">` + change_reason + `</textarea>
                                <a href="#" onclick="delchangeys(`+ q + `);"><font color="#FF0000">删除</font></a>
                            </td>
                        </tr>
                    </table></div>`;
            $("#changeysInfo").append(str);

            q++;
        }

        //删除变更与更正事项
        function delchangeys(no) {
			if (confirm("确定执行变更与更正事项删除操作吗？"))
			{		
				let id = $("#id" + no).val();
				if (id != 0) {
					let tjdNos = new Object();
					tjdNos.id = id;
					jQuery.ajax({
						//请求方式
						type: "POST",
						//请求地址
						url: "/node/delete_app_dfs_zxd_cschange",
						//数据，json对象
						data: tjdNos,
						//请求成功
						success: function (data) {
							if (data.code == "1") {
								$("#changeysDiv" + no).remove();
								alert("变更与更正事项删除成功！");
							} else if (data.code == "3") {
								alert("变更与更正事项ID为空！");
							} else {
								alert("变更与更正事项删除失败！");
							}
						},
						//请求失败，包含具体的错误信息
						error: function (e) {
							alert("数据请求异常！");
						}
					});
				} else {
					$("#changeysDiv" + no).remove();
				}
			}
        }
		//保存之前必输项校验
		function requiredCheck(){
			let value = "0";
            $("input[name^='changeysNo']").each(function name(params) {
                let no = $(this).val();
				let tjdNos = new Object();
				tjdNos.change_type = $("#change_type" + no).val();
				tjdNos.change_adesc = $("#change_adesc" + no).val();
				tjdNos.change_reason = $("#change_reason" + no).val();
				if(typeof(tjdNos.change_type)=="undefined" || tjdNos.change_type.length==0  || tjdNos.change_type.length=='')
				{
					alert("字段[变更类型/更正字段]为必填项!");
					value="1";
				}
				if(typeof(tjdNos.change_adesc)=="undefined" || tjdNos.change_adesc.length==0  || tjdNos.change_adesc.length=='')
				{
					alert("字段[变更事项/更正后值]为必填项!");
					value="1";
				}
				if(typeof(tjdNos.change_reason)=="undefined" || tjdNos.change_reason.length==0  || tjdNos.change_reason.length=='')
				{
					alert("字段[变更/更正原因]为必填项!");
					value="1";
				}
			});
			
			return value;
		}	
        //保存变更与更正事项
        function savechangeys(uuid, bsid, regitem_id, productid, productcode) {
            let code = "";		
            $("input[name^='changeysNo']").each(function name(params) {
                let no = $(this).val();
                let tjdNos = new Object();
                tjdNos.id = $("#id" + no).val();
                tjdNos.uuid = uuid;
                tjdNos.bsid = bsid;
                tjdNos.regitem_id = regitem_id;
                tjdNos.productid = productid;
                tjdNos.productcode = productcode;
                tjdNos.change_type = $("#change_type" + no).val();				
                tjdNos.change_bdesc = $("#change_bdesc" + no).val();
                tjdNos.change_adesc = $("#change_adesc" + no).val();
				tjdNos.change_reason = $("#change_reason" + no).val();
                jQuery.ajax({
                    //请求方式
                    type: "POST",
                    //请求地址
                    url: "/node/insert_app_dfs_zxd_cschange",
                    //数据，json对象
                    data: tjdNos,
                    //请求成功
                    success: function (data) {
                        if (data.code != code) {
							if(no==1){
								if (data.code == "1") {
									alert("变更与更正事项保存成功！");
									location.reload();
								} else {
									alert("变更与更正事项保存失败！");
								}
							}
                        }
                        code = data.code;
                    },
                    //请求失败，包含具体的错误信息
                    error: function (e) {
                        alert("数据请求异常！");
                    }
                });
            });
        }
    
        //获取GET参数
        function GetQueryValue(queryName) {
            var query = decodeURI(window.location.search.substring(1));
            var vars = query.split("&");
            for (var i = 0; i < vars.length; i++) {
                var pair = vars[i].split("=");
                if (pair[0] == queryName) { return pair[1]; }
            }
            return null;
        }
    </script>
</html>